package com.kdtech.analyse.NewsPaper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.Answer.AnalyseAnswer;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * 长江日报  499
 * @author KK
 *
 */
public class CnhanNewsPaperAnalyse implements AnalyseNews {


	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://cjrb.cnhan.com/[0-9]{4}/cjrb_[0-9]*/[0-9]*.html",
				"http://whcb.cnhan.com/[0-9]{4}/whcb_[0-9]*/[0-9]*.html",
				"http://whwb.cnhan.com/[0-9]{4}/whwb_[0-9]*/[0-9]*.html",
				"http://www.cnhan.com/html/[0-9]{4}/ctjb_[0-9]*/[0-9]*.html",
				"http://www.cnhan.com/html/whcb/[0-9]{8}/[0-9]*.htm",
				"http://www.cnhan.com/html/whwb/[0-9]{8}/[0-9]*.htm"
				};
		//http://www.cnhan.com/html/whwb/20161103/452751.htm
		//http://www.cnhan.com/html/whcb/20161103/452705.htm
		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();


		String title = null;
		String content = null;
		Long date = null;
		String dateStr = null;
		Document doc = Jsoup.parse(html);

		title =doc.select("title").text();
		if (title!=null){
			title = StringUtils.substringBeforeLast(title, "-");
		}
		dateStr = url;
		date=DateUtils.matchDate(dateStr);

		content =HtmlCleaner.getContentHtml(url,doc.select("article.post-article"));
		if(StringUtils.isBlank(content)){
			content =HtmlCleaner.getContentHtml(url,doc.select("div#post-article"));
		}

			NewsMeta newspaper = new NewsMeta();
			newspaper.setUrl(url);
			newspaper.setTitle(StringUtils.trimSpace(title));
			newspaper.setContent(StringUtils.trimSpace(content));
			newspaper.setDate(date);
			return newspaper;

	}
	

	

	
}
